package com.mamesoft.namibiamc.tools;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;


public class JDBC {

    /**
     * @param args the command line arguments
     */
    static final String HOST = "localhost:3306";
    static final String DB_NAME = "ppw_progetto2";
    static final String USER = "root";
    static final String PSSWD = "root";

    public static Connection connect() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Connection dbConnection = null;
        String dbString = "jdbc:mysql://" + HOST + "/" + DB_NAME;
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            dbConnection = (Connection) DriverManager.getConnection(dbString, USER, PSSWD);
        
        return dbConnection;
    }
    
    public static void executeStatement(String q, Connection c) throws SQLException {

            Statement s = (Statement) c.createStatement();
            s.execute(q);
            s.close();
    }
    
    public static ResultSet executeQuery(String q, Connection c) throws SQLException {

            Statement s = (Statement) c.createStatement();
            ResultSet res = (ResultSet) s.executeQuery(q);
            return res;
    }
}
